package net.zhengxinyun.performance.App.service.deptIndex;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.dto.DepartIndexDataDTO;
import net.zhengxinyun.performance.App.mapper.DeptIndexMapper;
import net.zhengxinyun.performance.App.service.BaseIndexService.BaseIndexService;
import net.zhengxinyun.performance.Util.DataUtil;
import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.entity.CountryUnitScoreEntity;
import net.zhengxinyun.performance.filter.CountryUnitScoreFilter;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.mapper.Indicators.CountryUnitScoreEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 县直单位
 */
@Service
public class DeptIndexServiceImpl extends BaseIndexService implements DeptIndexService {

    @Autowired
    private CountryUnitScoreEntityMapper deptScoreMapper;
    @Autowired
    private DeptIndexMapper  deptIndexMapper;

    @Override
    public List<CountryUnitScoreFilter> getDeptIndexs(CountryUnitScoreFilter filter, UserFilter userFilter) {

        List<CountryUnitScoreFilter> deptIndesLists = deptScoreMapper.queryScoreHomeDate(filter);
        List<CountryUnitScoreFilter> filterIndexs = filterDeptInditors(deptIndesLists, userFilter);

        return filterIndexs;
    }
    /**
     * 单个指标的得分情况
     * @param id
     * @param datetime
     * @return
     */
    @Override
    public List<DepartIndexDataDTO> getSingleIndexScore(Long id, String datetime) {

        List<CountryUnitScoreFilter> list = deptIndexMapper.getSingleIndexScore(id, datetime,null);
        List<DepartIndexDataDTO> datas = new ArrayList<>();
        for (CountryUnitScoreFilter item : list) {

            datas.add(new DepartIndexDataDTO(null,item.getDepartment(),DataUtil.doubleParse(item.getScore() ) ,0,   Long.parseLong(item.getIndexId()) , datetime,item.getDepartCode()) );

        }
      /*  log.info("---------排序前------");
        datas.forEach(o-> System.out.println(o.getFinalScore()) );
        //排序
        datas.sort(Comparator.comparing(DepartIndexDataDTO::getFinalScore ).reversed()  );
        log.info("-------排序后-------");
        datas.forEach(o-> System.out.println(o.getFinalScore()) );*/
        //计算排名 并列
        List<Map.Entry<Double, List<DepartIndexDataDTO>>> newDatas = datas.stream().collect(Collectors.groupingBy(DepartIndexDataDTO::getFinalScore)).entrySet().stream()
                .sorted((o1, o2) -> -Double.compare(o1.getKey(), o2.getKey())).collect(Collectors.toList());
        int rank =1;
        // 新计算并列排名
        for (Map.Entry<Double, List<DepartIndexDataDTO>> newData : newDatas) {

            int finalRank = rank;
            newData.getValue().forEach(departIndexDataDTO -> departIndexDataDTO.setRank(finalRank));
                    rank=rank+ newData.getValue().size();


        }
        return datas;
    }

    //单个指标的所有月份得分
    @Override
    public List<DepartIndexDataDTO> IndexWholeScore(Long id, String departCode) {
        List<DepartIndexDataDTO> datas  = new ArrayList<>();
        List<CountryUnitScoreFilter> list = deptIndexMapper.getSingleIndexScore(id, null,departCode);
        for (CountryUnitScoreFilter item : list) {

            datas.add( new DepartIndexDataDTO(item.getDepartment(),DataUtil.doubleParse(item.getScore()),
                    id,item.getDateTime(),item.getDepartCode())  );

        }


        return datas;
    }


    @Override
    @Transactional
    public String saveDeptScores(List<CountryUnitScoreEntity> list, Long id) {

        CountryUnitScoreFilter filter  = new CountryUnitScoreFilter();
        filter.setId(id);
        filter.setDateTime(DateUtils.getLastMonth());
        filter.setIndexId(id.toString());
        List<CountryUnitScoreEntity>  templist = deptScoreMapper.selectByIndexId(filter);
        if(ObjectUtils.isNotBlank(templist))
        {
            for ( CountryUnitScoreEntity item: templist)
            {
                item.setVaild("0");
                deptScoreMapper.updateByPrimaryKey(item);
            }
        }

        for (CountryUnitScoreEntity item : list) {


            item.setDateTime(DateUtils.getLastMonth());
            item.setAudit("1");
            item.setVaild("1");
            deptScoreMapper.insert(item);

        }
        return  "ok";



    }
}
